using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using TTDotNet.Engine.Stations;

namespace TTDotNet.Engine.Files
{
    public class StationLoader : FileLoader
    {

        public StationLoader() : base("../../data/stations.xml")
        {

        }

        public Dictionary<StationType, StationInfo> GetStationInfo()
        {
            Dictionary<StationType, StationInfo> stationInfos = new Dictionary<StationType, StationInfo>();

            try
            {
                reader.ReadStartElement("Stations");

                while (reader.IsStartElement())
                {
                    reader.ReadStartElement("Station");
                    reader.ReadStartElement("Name");
                    string name = reader.ReadContentAsString();
                    reader.ReadEndElement();
                    reader.ReadStartElement("Type");
                    StationType sType = (StationType)Enum.Parse(typeof(StationType), reader.ReadContentAsString());
                    reader.ReadEndElement();

                    reader.ReadStartElement("Meshes");

                    List<BuildingInfo> buildingInfos = new List<BuildingInfo>();

                    while(reader.Name.Equals("Mesh")) {

                        reader.Read();

                        reader.ReadStartElement("Path");
                        string meshPath = reader.ReadContentAsString();
                        reader.ReadEndElement();

                        reader.ReadStartElement("Width");
                        int width = reader.ReadContentAsInt();
                        reader.ReadEndElement();

                        reader.ReadStartElement("Length");
                        int length = reader.ReadContentAsInt();
                        reader.ReadEndElement();

                        reader.ReadStartElement("Occupies");
                        string bitmask = reader.ReadContentAsString();
                        BitArray bitArray = new BitArray(bitmask.Length);

                        for (int i = 0; i < bitArray.Length; i++)
                        {
                            bitArray[i] = bitmask.Substring(i, 1).Equals("1");
                        }

                        BuildingInfo bInfo = new BuildingInfo(meshPath, new Size(width, length), bitArray);
                        buildingInfos.Add(bInfo);

                        reader.ReadEndElement();
                        reader.ReadEndElement();

                    }

                    reader.ReadEndElement();

                    reader.ReadStartElement("CatchmentDepth");
                    int depth = reader.ReadContentAsInt();
                    reader.ReadEndElement();
                    reader.ReadEndElement();


                    StationInfo sInfo = new StationInfo(name, depth, buildingInfos);

                    stationInfos[sType] = sInfo;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error parsing XML, " + e.Message);
                Environment.Exit(1);
                return null;
            }

            return stationInfos;

        }
    }
}
